package demo02;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

public class FoodManager {
    //定义一个用来存储的仓库
    private Food[] arr = {};
    //设置数组的默认初始长度
    private  int  default_capability=5;

    public FoodManager(){
        //学号从1000开始

        Food f1 = new Food("牛肉干",12,5);
        Food f2 = new Food("果冻",4,7);
        Food f3 = new Food("豆腐干",2,2);
        this.add(f1);
        this.add(f2);
        this.add(f3);

    }

    //用来储存实际保存的食品种类数量
    private  int size;

    public boolean add(Food food){
        for (int i=0;i<size;i++){
            if (food.getName().equals(arr[i].getName())){
                arr[i].setPrice(food.getPrice());
                arr[i].setInventory(arr[i].getInventory()+food.getInventory());
                return true;
            }
        }
        if (size+1>arr.length){
            int min=Math.min(size,default_capability);
            int capability;
            if (min<default_capability){
                capability=default_capability;
            }else{
                capability=size+(size >> 1);
            }
            Food[]  newArr=new Food[capability];
            System.arraycopy(this.arr,0,newArr,0,size);
            this.arr=newArr;
        }
        arr[size++] = food;
        return true;
    }

    public Food[] findByName(String name){
        Food[] fooArr=new Food[1];
        for (int i=0;i<size;i++){
            Food food=arr[i];
            if (food.getName().equals(name)){
                fooArr[0]=food;
            }
        }
        return fooArr;
    }

    public Food[] findAll(){
        Food[] foods=new Food[size];
        System.arraycopy(arr,0,foods,0,size);
        return foods;
    }

    public boolean updateByName(String name){
        for (int i=0;i<size;i++){
            if (arr[i].getName().equals(name)){
                return true;
            }
        }
        return false;
    }
    public boolean updateByName(String name,double price,int inventory){
        for (int i=0;i<size;i++){
            if (arr[i].getName().equals(name)){
                arr[i].setPrice(price);
                arr[i].setInventory(inventory);
                return true;
            }
        }
        return false;
    }


    public double countPrice(){
        double sum=0;
        for (int i=0;i<size;i++){
            Food food=arr[i];
            sum+= food.getInventory()*food.getPrice();
        }
        return sum;
    }

    public Food[] biggestPriceFoodL(){
        //1.确定最贵商品的价格
        //2.确定最贵商品价格的数量
        //3.创建新数组，长度为第二部得到的长度
        //4.将价格最贵的商品添加到第三步创建的数组
        //5.返回新数组

        Food[] foods = Arrays.copyOf(arr, size);

        double max=0;
        for (int i = 0; i < foods.length - 1;i++) {
            if(foods[i].getPrice()>max){
                max=foods[i].getPrice();
            }
        }

        int count=0;
        for (int i = 0; i < foods.length - 1;i++) {
            if (foods[i].getPrice()==max){
                count++;
            }
        }

        Food[] foods1=new Food[count];
        int index=0;
        for (int i = 0; i < foods.length - 1;i++) {
            if (foods[i].getPrice()==max){
                foods1[index++]=foods[i];
            }
        }
        return foods1;
    }

    public Food[] sortByPrice(){
        Food[] foods = Arrays.copyOf(arr, size);
        //冒泡排序
        for (int i = 0; i < foods.length - 1;i++) {
            for (int j = 0; j < foods.length - 1 - i; j++) {
                Food t1 = foods[j];
                Food t2 = foods[j + 1];
                if (t1.getPrice() > t2.getPrice()) {
                    Food temp = t1;
                    foods[j] = t2;
                    foods[j + 1] = temp;
                }
            }
        }
        return foods;
    }
}
